<?php

class TutorLogin extends Controller {
	var $msg = '';
	
	function TutorLogin()
	{
		parent::Controller();
	}

	function index()
	{
	 	if ($this->session->userdata('logged_in') != TRUE || $this->session->userdata('profile') != 'Tutor')
	    {
	        redirect('login/index');
	    }
	    $this->load->model('TutorModel');
	    $this->load->model('StudentModel');
	    $this->load->model('NewsModel');
	    $tt = $this->TutorModel->getByUserName($this->session->userdata('username'));
		$monthTh = array("","มกราคม","กุมภาพันธ์ ","มีนาคม","เมษายน","พฤษภาคม","มิถุนายน","กรกฎาคม","สิงหาคม","กันยายน"," ตุลาคม","พฤศจิกายน","ธันวาคม");
		$data['news'] = $this->NewsModel->getTutorNews();	    
	    $data['tutor_id'] = $tt['tutor_id'];
	    $data['tutor_name'] =  $tt['first_name_th'] . ' ' . $tt['last_name_th'];
	    $data['expire_date'] = strftime("%d",strtotime($tt['user_expire'])) . ' ' .
	    					   $monthTh[ (int) strftime("%m",strtotime($tt['user_expire']))] . ' ' .
	    					   ( (int) strftime("%Y",strtotime($tt['user_expire'])) + 543);
	    					   
		$data['select_stu_mon'] = $this->StudentModel->getStuForTutor('mon');
		$data['select_stu_tue'] = $this->StudentModel->getStuForTutor('tue');
		$data['select_stu_wed'] = $this->StudentModel->getStuForTutor('wed');
		$data['select_stu_thr'] = $this->StudentModel->getStuForTutor('thr');
		$data['select_stu_fri'] = $this->StudentModel->getStuForTutor('fri');
		$data['select_stu_sat'] = $this->StudentModel->getStuForTutor('sat');
		$data['select_stu_sun'] = $this->StudentModel->getStuForTutor('sun');
		
		$data['selected_stu'] = $this->StudentModel->selectedStuForTutor($tt['tutor_id'], 'mon');
		$this->load->model('filemanagementmodel');
	    $data['header_tel'] = $this->filemanagementmodel->getByPostion('header_tel');
	    $data['footer_address'] = $this->filemanagementmodel->getByPostion('footer_add1');
	    $data['banner'] = $this->filemanagementmodel->getByPostion('tutor_login');
	    if($this->msg != '') {
	    	$data['success'] = $this->msg ;
	    }
	    $this->load->view('f_header', $data);
		$this->load->view('f_tutor_login', $data);
		$this->load->view('f_footer', $data);
	}
	
	function cancel() {
		if ($this->session->userdata('logged_in') != TRUE || $this->session->userdata('profile') != 'Tutor')
	    {
	        redirect('login/index');
	    }
	    $tutorID = $this->input->post('tutorID');
	    $this->load->model('TutorModel');
	    $this->load->model('StudentModel');
	     if(isset($tutorID)) {
			$cancelArr = $this->input->post('cancel_chk');
			
			if(isset($cancelArr) && empty($cancelArr) == false) {
				foreach ($cancelArr as $r) {
					$tmps = explode(",", $r);
					$d = array();
					$d['student_id'] = $tmps[0];
					$d['dayy'] = $tmps[1];
					$d['match_tutor_id'] = $tutorID;
					$d['maching_id'] = $tmps[2];
					$d['t_id'] = $tmps[3];
					$d['maching_status'] = 'อยู่ระหว่างการคัดเลือกติวเตอร์';
					$this->StudentModel->cancelBooking($d);
					$this->StudentModel->removeLog($d);
				}
			}
	    }
	   $this->msg = 'บันทึกข้อมูลเรียบร้อยแล้วค่ะ';
	   $this->index();
	}
	
	function save()
	{
	 	if ($this->session->userdata('logged_in') != TRUE || $this->session->userdata('profile') != 'Tutor')
	    {
	        redirect('login/index');
	    }
	    $tutorID = $this->input->post('tutorID');
	    $this->load->model('TutorModel');
	    $this->load->model('StudentModel');
	    
	    if(isset($tutorID)) {
			$confirmArr = $this->input->post('selected_chk');
			$subjArr = $this->input->post('stu_subj_detail');
			$timeArr = $this->input->post('stu_time_detail');
	    	if(isset($confirmArr) && empty($confirmArr) == false) {
				for($xx = 0; $xx < count($confirmArr); $xx++) {
					$r = $confirmArr[$xx];
					$tmps = explode(",", $r);
					$d = array();
					$d['student_id'] = $tmps[0];
					$d['dayy'] = $tmps[1];
					$d['maching_status'] = 'อยู่ในระหว่างการติดต่อ';
					$d['match_tutor_id'] = $tutorID;
					$d['subject'] = $subjArr[$xx];
					$d['time'] = $timeArr[$xx];
					$this->StudentModel->confirmBooking($d);
					$this->StudentModel->saveLog($d);
				}
			}
	    }
	    $this->msg = 'บันทึกข้อมูลเรียบร้อยแล้วค่ะ';
	   $this->index();
	}
	
	function savePassword(){
		if ($this->session->userdata('logged_in') != TRUE)
	    {
	        redirect('login/index');
	    }
	    $this->load->model('usermodel');
	    $this->load->helper(array('form', 'url', 'security'));
	    $passwordOld = $this->input->post('txtOldPass');
	    $passwordNew1 = $this->input->post('txtNewPass1');
	    $passwordNew2 = $this->input->post('txtNewPass2');
	    
	    $data = $this->usermodel->getUserByUsername($this->session->userdata('username'));
		if(dohash($passwordOld) == $data['password'] && $passwordNew1 == $passwordNew2) {
			$userdata['username'] = $this->session->userdata('username');
			$userdata['password'] = $passwordNew1;
			$this->usermodel->updateUser($userdata);
			$data['success'] = "บันทึกข้อมูลเรียบร้อย";
			$this->session->sess_destroy();
			redirect('login/index/');
		} else {
			$data['success'] = "ไม่สามารถบันทึกข้อมูลได้: รหัสผ่านเดิมไม่ถูกต้อง";
			$this->load->model('TutorModel');
		    $this->load->model('StudentModel');
		    $this->load->model('NewsModel');
			$tt = $this->TutorModel->getByUserName($this->session->userdata('username'));
			$monthTh = array("","มกราคม","กุมภาพันธ์ ","มีนาคม","เมษายน","พฤษภาคม","มิถุนายน","กรกฎาคม","สิงหาคม","กันยายน"," ตุลาคม","พฤศจิกายน","ธันวาคม");
			 	    
		    $data['tutor_id'] = $tt['tutor_id'];
		    $data['tutor_name'] =  $tt['first_name_th'] . ' ' . $tt['last_name_th'];
		    $data['expire_date'] = strftime("%d",strtotime($tt['user_expire'])) . ' ' .
		    					   $monthTh[ (int) strftime("%m",strtotime($tt['user_expire']))] . ' ' .
		    					   ( (int) strftime("%Y",strtotime($tt['user_expire'])) + 543);
			$data['select_stu'] = $this->StudentModel->getStuForTutor();
			$data['selected_stu'] = $this->StudentModel->selectedStuForTutor($tt['tutor_id']);
			
			$this->load->model('filemanagementmodel');
		    $data['header_tel'] = $this->filemanagementmodel->getByPostion('header_tel');
		    $data['footer_address'] = $this->filemanagementmodel->getByPostion('footer_add1');
		    $data['news'] = $this->NewsModel->getTutorNews();
		    $this->load->view('f_header', $data);
			$this->load->view('f_tutor_login', $data);
			$this->load->view('f_footer', $data);
		}
	    
	      
	    
	    
	   
	}
	
	function edit() 
	{
	if ($this->session->userdata('logged_in') != TRUE)
	    {
	        redirect('login/index');
	    }
	    
	    
	    $this->load->model('filemanagementmodel');
	    $data['banner'] = $this->filemanagementmodel->getByPostion('tutor_login');
	    
		$this->load->view('header');
		$this->load->view('tutor-attach-form', $data);
		$this->load->view('footer');
	}
	
	function upload()
	{
	 	if ($this->session->userdata('logged_in') != TRUE)
	    {
	        redirect('login/index');
	    }
	    
	    $this->load->model('filemanagementmodel');
	    $s = array();
	    
		// save profile image
		if(isset($_FILES) && @$_FILES['txtFiles']['error'] == '0') {
			$config['upload_path'] = './data-image/file_management/';
			$config['allowed_types'] = 'gif|jpg|png|zip|pdf|doc|ppt|xls';
			$config['max_size']	= '3048';
			$config['overwrite'] = TRUE;
			$config['file_name'] = 'tutor_login';
			$this->load->library('upload', $config);
			if ( ! $this->upload->do_upload('txtFiles'))
			{
				$this->msg = "ไม่สามารถ upload ได้";			
			}
			else
			{
				$data = array('upload_data' => $this->upload->data());
				$Imgdata = array(
				   'position' => 'tutor_login',
	               'file_name' => $data['upload_data']['file_name']   
	            );
				$this->filemanagementmodel->update($Imgdata);
				$this->msg = "บันทึกข้อมูลเรียบร้อย";
				$s['banner'] = $this->filemanagementmodel->getByPostion('tutor_login');
			}
		}
		 
		$this->load->view('header');
		$this->load->view('tutor-attach-form', $s);
		$this->load->view('footer', $this->msg);
	}
}

?>
